package lib.quasar.widget.line;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description: 底部一条线
 * created by kalu on 2017/5/16 18:35
 */
public class LineTextView extends AppCompatTextView {

    private boolean localtionTop = false;
    private boolean localtionBottom = false;
    private boolean localtionLeft = false;
    private boolean localtionRight = false;
    private boolean paintBold = false;

    private float paddingTop = 0;
    private float paddingBottom = 0;
    private float paddingLeft = 0;
    private float paddingRight = 0;

    private float lineHeight = 5;
    private int lineColor = Color.BLACK;

    private Paint paint;

    public LineTextView(Context context) {
        this(context, null, 0);
    }

    public LineTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();

        TypedArray array = null;

        try {

            array = context.obtainStyledAttributes(attrs, R.styleable.LineTextView);

            localtionTop = array.getBoolean(R.styleable.LineTextView_ltv_location_top, false);

            localtionTop = array.getBoolean(R.styleable.LineTextView_ltv_location_top, false);
            localtionBottom = array.getBoolean(R.styleable.LineTextView_ltv_location_bottom, false);
            localtionLeft = array.getBoolean(R.styleable.LineTextView_ltv_location_left, false);
            localtionRight = array.getBoolean(R.styleable.LineTextView_ltv_location_right, false);
            paintBold = array.getBoolean(R.styleable.LineTextView_ltv_paint_bold, false);

            paddingTop = array.getDimension(R.styleable.LineTextView_ltv_padding_top, 0);
            paddingBottom = array.getDimension(R.styleable.LineTextView_ltv_padding_bottom, 0);
            paddingLeft = array.getDimension(R.styleable.LineTextView_ltv_padding_left, 0);
            paddingRight = array.getDimension(R.styleable.LineTextView_ltv_padding_right, 0);

            lineHeight = array.getDimension(R.styleable.LineTextView_ltv_line_height, lineHeight);
            lineColor = array.getColor(R.styleable.LineTextView_ltv_line_color, lineColor);

        } catch (Exception e) {
            LogUtil.e("LineTextView", e.getMessage(), e);
        } finally {
            if (null != array) {
                array.recycle();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (paintBold) {
            getPaint().setFakeBoldText(true);
        }

        super.onDraw(canvas);

        int height = getHeight();
        int width = getWidth();

        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(lineColor);

        if (localtionTop) {
            canvas.drawRect(paddingLeft, 0, width - paddingRight, lineHeight, paint);
        }

        if (localtionBottom) {
            canvas.drawRect(paddingLeft, height - lineHeight, width - paddingRight, height, paint);
        }

        if (localtionLeft) {
            canvas.drawRect(paddingLeft, paddingTop, lineHeight, height - paddingBottom, paint);
        }

        if (localtionRight) {
            canvas.drawRect(width - paddingRight - lineHeight, paddingTop, width - paddingRight, height - paddingBottom, paint);
        }
    }

    @Override
    public void setText(CharSequence text, BufferType type) {

        if (paintBold) {
            getPaint().setFakeBoldText(true);
        }
        super.setText(text, type);
    }
}